Setup

library(ggplot2)

(p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Petal.Width)) + 
  geom_point(size = 3))

(p2 <- ggplot(mpg, aes(class, fill = class)) + 
  geom_bar(show.legend = FALSE))

Quick Overview

The theme_omics() function adds a custom theme to a ggplot.

(p1 <- p1 + theme_omics())

(p2 <- p2 + theme_omics())

There are dedicated scale functions for custom sequential, diverging and categorical color palettes based on the style guide. The function family scale_color|fill_omics_* can be used to apply these colors to color and fill aesthetics. Depending if the variables are continuous or discrete, the functions end with _c and _d, respectively.

(p1 <- p1 + scale_color_omics_c())

(p2 <- p2 + scale_fill_omics_d())

Available Color Palettes

Sequential Palettes

p1 + scale_color_omics_c(palette = "brand_blue")

Diverging Palettes

Diverging palettes are a gradient consisting of two hues that are blended towards the center values (midpoint). The palettes are designed in a way that colors usually associated with “bad” or “lows” such as red and orange are pointing towards the minimal values. Keep in mind that the midpoint should be picked with care and be meaningful to the reader (i.e. why are values below the midpoint different from those above the threshold?).

p1 + scale_color_omics_c(palette = "blue_red")

Categorical Palettes

All categorical color palettes are variants of the same color palette called default. The categorical palette is limited to a eight unique colors, with the first being the brand blue. For most use cases, categorical colors should be limited to 4-6 and never exceed 8 categories.

p2 + scale_fill_omics_d(palette = "default")

Working with Color Palettes

Reverse the Direction of a Palette

For sequential palettes, the default direction is the golden standard for most use cases: the highest values have the highest contrasts, i.e. the darkest shade on a light background. You can change that behavior by setting the reverse argument in the scale_*_omics_c() function to TRUE. reverting the direction can be useful when the viewer should focus on the lowest values or when using a dark background

p1 + scale_color_omics_c(reverse = TRUE)

The same applies to diverging palettes. As diverging palettes are getting darker towards both ends of the data range, changing the direction is uncomplicated in most cases. Keep in mind though that humans associate colors with feelings and features. The palettes are designed in a way that colors usually associated with “bad” or “lows” such as red and orange are pointing towards the minimal values.

p1 + scale_color_omics_c(palette = "blue_red_grey", reverse = TRUE)

Categorical palettes can be reversed as well. Keep in mind that by default the color for the first category is the brand color. When reversing the palette, the brand blue might be not be contained in your palette.

p2 + scale_fill_omics_d(reverse = TRUE)

Custom Order for Categorical Palettes

You can also build categorical palettes on your own, either by shuffling a palette (omics_pal_d) or by picking colors from the BigOmics the palettes or directly from the data colors (omics_colors).

my_cols <- omics_pal_d(palette = "default")(7)

set.seed(2022)
my_shuffled_cols <- sample(my_cols)
p2 + scale_fill_manual(values = my_shuffled_cols)

my_ordered_cols <- my_cols[c(2,1,5,3,7,4,6)]
p2 + scale_fill_manual(values = my_ordered_cols)

my_picked_colors <- omics_colors("brand_blue", "bright_blue", "super_dark_grey", 
                                 "mid_grey", "green", "orange", "red")
p2 + scale_fill_manual(values = unname(my_picked_colors))

Theme Settings

Change the Default style

Set style = "light" to use a light-toned theme.

p1 + theme_omics(style = "light")

p2 + theme_omics(style = "light")

Change the Size of Elements

base_size as in the ggplot2 function theme_*().

p1 + theme_omics(base_size = 25)

p2 + theme_omics(base_size = 8)

Change the Panel Grid

Use any combination of x, X, y, and Y for the grid argument to add major grid lines.

p1 + theme_omics(grid = "xy") ## "XY", "xY" or "Xy" also work

p2 + theme_omics(grid = "y") ## "Y" also works

Use none to remove all grid lines.

p1 + theme_omics(grid = "none") ## "None" also works

Handle Axis Titles

By adjusting axistitle, you can keep or remove single or all titles.

p1 + theme_omics(axistitle = "y") ## "Y" also works

p2 + theme_omics(axistitle = "none") ## "none" also works

Handle Axis Text

By adjusting axistext, you can keep or remove single or all text at the breaks.

p1 + theme_omics(axistext = "none") ## "None" also works

p2 + theme_omics(axistext = "y", axistitle = "y", grid = "y") ## "Y" also works

Add a Panel Border

The argument panelborder controls the absence (default) or presence of a panel border surrounding the data area.

p1 + theme_omics(panelborder = TRUE)

Add a Margin to a Plot

By default, there is no margin around the plot to ensure that the margin is controlled by the app settings so that the plot is as large as possible. You can easily ad some margin by changing the margin argument.

p1 + 
  theme_omics(margin = 5) +
  ## add an outline to illustrate the effect
  theme(plot.background = element_rect(color = "black"))

p2 + 
  theme_omics(margin = 50) +
  ## add an outline to illustrate the effect
  theme(plot.background = element_rect(color = "black"))

Session Info
## [1] "2023-02-03 15:12:37 CET"
## Local:    develop C:/Users/DataVizard/Google Drive/Work/DataViz/Clients/2022_BigOmics/omicsplayground
## Remote:   develop @ origin (https://github.com/bigomics/omicsplayground.git)
## Head:     [e1639b3] 2023-02-03: rename gugplot guide
## R version 4.1.0 (2021-05-18)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 22621)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
## [3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
## [5] LC_TIME=German_Germany.1252    
## system code page: 65001
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_3.3.6     systemfonts_1.0.3
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.1.2    xfun_0.31           bslib_0.4.0        
##  [4] purrr_0.3.4         colorspace_2.0-3    vctrs_0.4.1        
##  [7] generics_0.1.3      htmltools_0.5.2     yaml_2.2.1         
## [10] utf8_1.2.2          rlang_1.0.4         gridtext_0.1.4.9000
## [13] jquerylib_0.1.4     pillar_1.8.1        glue_1.6.2         
## [16] withr_2.5.0         DBI_1.1.2           lifecycle_1.0.1    
## [19] stringr_1.4.1       munsell_0.5.0       gtable_0.3.0       
## [22] ragg_1.2.2          evaluate_0.16       labeling_0.4.2     
## [25] knitr_1.39          fastmap_1.1.0       fansi_0.5.0        
## [28] highr_0.9           Rcpp_1.0.7          scales_1.2.1       
## [31] cachem_1.0.6        jsonlite_1.7.2      farver_2.1.0       
## [34] textshaping_0.3.6   digest_0.6.29       stringi_1.7.5      
## [37] dplyr_1.0.9         grid_4.1.0          rprojroot_2.0.3    
## [40] here_1.0.1          cli_3.3.0           tools_4.1.0        
## [43] magrittr_2.0.3      sass_0.4.2          tibble_3.1.8       
## [46] pkgconfig_2.0.3     xml2_1.3.3          assertthat_0.2.1   
## [49] rmarkdown_2.16      rstudioapi_0.14     R6_2.5.1           
## [52] git2r_0.29.0        ggtext_0.1.1        compiler_4.1.0